package org.chen.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.exception.ExceptionCodeEnum;
import org.chen.common.model.travel.StoreTravelOrder;
import org.chen.common.request.OrderRefundApplyRequest;
import org.chen.common.request.OrderTravelPayRequest;
import org.chen.common.response.CommonResult;
import org.chen.common.response.OrderTravelPayResultResponse;
import org.chen.common.utils.FlyingUtil;
import org.chen.service.OrderPayTravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("api/pay")
@Tag(name = "支付", description = "支付管理")
public class PayController {

    @Autowired
    private OrderPayTravelService orderPayTravelService;

    /**
     * 生成paypal的支付订单(旅游)
     */
    @Operation(summary = "生成paypal的支付订单")
    @RequestMapping(value = "/payment/travel", method = RequestMethod.POST)
    public CommonResult<OrderTravelPayResultResponse> paymentTravel(@RequestBody @Validated OrderTravelPayRequest orderPayRequest, HttpServletRequest request) {
        String ip = FlyingUtil.getClientIp(request);
        return CommonResult.success(orderPayTravelService.payment(orderPayRequest, ip));
    }

    /**
     * 转移资金(回调接口旅游)
     */
    @PostMapping("/capture-paypal-order")
    public ResponseEntity<?> capturePayPalOrder(@RequestBody StoreTravelOrder storeTravelOrder) {
        return ResponseEntity.ok(orderPayTravelService.paySuccess(storeTravelOrder));
    }

    /**
     * 查询支付结果（旅游）
     *
     * @param orderNo |订单编号|String|必填
     */
    @Operation(summary = "查询支付结果")
    @RequestMapping(value = "/queryPayResult/travel", method = RequestMethod.GET)
    public CommonResult<String> queryPayResultTravel(@RequestParam String orderNo) throws IOException {
        return CommonResult.success(orderPayTravelService.queryPayResult(orderNo));
    }
}
